package com.wenhui.schoolyardinfo.mine.ui;

import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.qmuiteam.qmui.widget.QMUITopBar;
import com.wenhui.schoolyardinfo.R;
import com.wenhui.schoolyardinfo.base.BaseActivity;
import com.wenhui.schoolyardinfo.firstpage.adapter.BaseRecyclerAdapter;
import com.wenhui.schoolyardinfo.firstpage.holder.SmartViewContentHolder;
import com.wenhui.schoolyardinfo.manager.LogManager;
import com.wenhui.schoolyardinfo.release.bean.JurisdictionBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * author    : Urasaki
 * e-mail    : 1164688204@qq.com
 * date      : 2019/7/6 11:02
 * introduce : 我的相册权限Activity
 */

public class JurisdictionMineAlbumActivity extends BaseActivity {

    private static final String TAG = "JurisdictionMineAlbumActivity";
    @BindView(R.id.imv_back)
    ImageView imvBack;
    @BindView(R.id.layout_back)
    RelativeLayout layoutBack;
    @BindView(R.id.topbar)
    QMUITopBar topbar;
    @BindView(R.id.mine_recycler_view)
    RecyclerView mineRecyclerView;

    private BaseRecyclerAdapter<JurisdictionBean> baseRecyclerAdapter;
    private LinearLayoutManager layoutManager;
    private List<JurisdictionBean> jurisdictionBeanList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jurisdiction_mine_album);
        ButterKnife.bind(this);

        initData();
        initViews();
    }

    @Override
    protected void initData() {
        jurisdictionBeanList = new ArrayList<>();
        jurisdictionBeanList.add(new JurisdictionBean("所有用户", true));
        jurisdictionBeanList.add(new JurisdictionBean("我的好友", false));
        jurisdictionBeanList.add(new JurisdictionBean("仅自己可见", false));
    }

    @Override
    protected void initViews() {
        setTopBar(topbar);

        initAdapter();
    }

    private void initAdapter() {
        layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        mineRecyclerView.setLayoutManager(layoutManager);
        mineRecyclerView.setItemAnimator(new DefaultItemAnimator());

        baseRecyclerAdapter = new BaseRecyclerAdapter<JurisdictionBean>(R.layout.item_jurisdiction, 1) {
            @Override
            protected void onBindViewHolder(RecyclerView.ViewHolder holder, JurisdictionBean model, int position) {
                if (holder instanceof SmartViewContentHolder) {
                    SmartViewContentHolder smartViewContentHolder = (SmartViewContentHolder) holder;
                    smartViewContentHolder.text(R.id.tev_name, model.getName());
                    ImageView imvCheckMark = smartViewContentHolder.getView(R.id.imv_check_mark);
                    LogManager.i(TAG, "onBindViewHolder a*****" + position);
                    if (model.isSelected()) {
                        LogManager.i(TAG, "onBindViewHolder b*****" + position);
                        imvCheckMark.setVisibility(View.VISIBLE);
                    } else {
                        imvCheckMark.setVisibility(View.GONE);
                    }
                }
            }
        };

        mineRecyclerView.setAdapter(baseRecyclerAdapter);
        baseRecyclerAdapter.clearData();
        baseRecyclerAdapter.addAllData(jurisdictionBeanList);
        baseRecyclerAdapter.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                LogManager.i(TAG, "position*****" + position);
                for (int i = 0; i < jurisdictionBeanList.size(); i++) {
                    if (i == position) {
                        jurisdictionBeanList.get(i).setSelected(true);
                    } else {
                        jurisdictionBeanList.get(i).setSelected(false);
                    }
                }

                baseRecyclerAdapter.clearData();
                baseRecyclerAdapter.addAllData(jurisdictionBeanList);
            }
        });
    }

    @OnClick(R.id.layout_back)
    public void onViewClicked() {
        finish();
    }
}
